package com.wzy.wzyblog.web;


import com.wzy.wzyblog.po.Type;
import com.wzy.wzyblog.service.BlogService;
import com.wzy.wzyblog.service.TypeService;
import com.wzy.wzyblog.vo.BlogQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;


@Controller
public class TypeShowController {

    @Autowired
    private TypeService typeService;

    @Autowired
    private BlogService blogService;

    @GetMapping("/types/{id}")
    public String types(@PageableDefault(size = 8, sort = {"updateTime"}, direction = Sort.Direction.DESC) Pageable pageable,
                        @PathVariable Long id, Model model) {
//        获取分类所有数据          类似首页分类展示（数目够大就不会限制 相当于查询全表）
        List<Type> types = typeService.listTypeTop(10000);
//        从首页点进的时候
        if (id == -1) {
//            显示第一个分类为选中状态  并获取id
           id = types.get(0).getId();
        }
//        把id放入blogQuery
        BlogQuery blogQuery = new BlogQuery();
        blogQuery.setTypeId(id);
//        显示所有的分类
        model.addAttribute("types", types);
//        根据id查询blog内容
        model.addAttribute("page", blogService.listBlog(pageable, blogQuery));
//       把选中状态的type的id传送到页面上
        model.addAttribute("activeTypeId", id);
        return "types";
    }
}
